/**
 * Copyright (C) 2011 Andrew C. Love (DNC) <dnc.app.sup@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dnc.cloak.framework.security.encryption;

import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

import javax.crypto.Cipher;

import org.apache.log4j.Logger;

public class AsymmetricKeyEncryptor implements EncryptionProvider {
    private static final Logger logger = Logger.getLogger(AsymmetricKeyEncryptor.class.getName());
    // Members
    private String keystoreName;
    private String transformation;
    private String algorithm;
    private String providerClassName;


    public AsymmetricKeyEncryptor() {
        super();
    }

    public AsymmetricKeyEncryptor(String keystoreName, String transformation, String algorithm, String providerClassName) {
        super();
        this.keystoreName = keystoreName;
        this.transformation = transformation;
        this.algorithm = algorithm;
        this.providerClassName = providerClassName;
    }

    public byte[] encryptByteArray(Key key, byte[] secret) throws Exception {
        return encryptByteArray((PublicKey) key, secret);
    }

    public byte[] encryptByteArray(PublicKey publicKey, byte[] secret) throws Exception {
        Cipher ciph = Cipher.getInstance(transformation);
        ciph.init(Cipher.ENCRYPT_MODE, publicKey, new SecureRandom());
        byte[] encrypted = ciph.doFinal(secret);
        return encrypted;
    }

    public byte[] decryptByteArray(Key key, byte[] secret) throws Exception {
        return decryptByteArray((PrivateKey) key, secret);
    }

    public byte[] decryptByteArray(PrivateKey privateKey, byte[] secret) throws Exception {
        Cipher ciph = Cipher.getInstance(transformation);
        ciph.init(Cipher.DECRYPT_MODE, privateKey, new SecureRandom());
        byte[] decrypted = ciph.doFinal(secret);
        return decrypted;
    }
    
    @Override
    public String getKeystoreName() {
        return keystoreName;
    }

    @Override
    public void setKeystoreName(String keystoreName) {
        this.keystoreName = keystoreName;
    }

    @Override
    public String getTransformation() {
        return transformation;
    }

    @Override
    public void setTransformation(String transformation) {
        this.transformation = transformation;
    }

    @Override
    public String getAlgorithm() {
        return algorithm;
    }

    @Override
    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }

    @Override
    public String getProviderClassName() {
        return providerClassName;
    }

    @Override
    public void setProviderClassName(String providerClassName) {
        this.providerClassName = providerClassName;
    }     

}
